/* The main part of the battle panel. Contains 8 Ship Panels used for battling
 * 
 */

package gui.battle;

import gui.GameGUI;
import item.ship.*;
import item.ship.resource.AntlionFlagship;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class ShipBattlePanel extends JPanel {


	ArrayList<ShipPanelLeft> leftShips; //the ships on the left
	ArrayList<ShipPanelRight> rightShips; //the ships on the right


	//0 param constructor that sets up the panel
	public ShipBattlePanel(ArrayList<Ship> l, ArrayList<Ship> r){

		this.setMaximumSize(new Dimension(1200, 510));
		this.setLayout(new GridLayout(4, 4, 0 ,10));

		leftShips = new ArrayList<ShipPanelLeft>(l.size());
		rightShips = new ArrayList<ShipPanelRight>(r.size());

		for(Ship b: l){

			ShipPanelLeft spl = new ShipPanelLeft(b);

			spl.addMouseListener(new MouseAdapter(){

				//when the panel is clicked
				public void mouseClicked(MouseEvent e){

					//to see if you can attack
					if(BattlePanel.selectedWeapon != null){

						if(isOnRight(BattlePanel.turn.getShip())){
							BattlePanel.turn.getShip().attack(BattlePanel.selectedWeapon, ((ShipPanelLeft)e.getComponent()).getShip());

							if(((ShipPanelLeft)e.getComponent()).getShip().getHull() == 0){

								GameGUI.battle.kill(((ShipPanelLeft)e.getComponent()).getShip());
								((ShipPanelLeft)e.getComponent()).setVisible(false);
							}//if

							((ShipPanelLeft)e.getComponent()).update();
							BattlePanel.sip.updateActionPoints();
							return;
						}//if
					}//if

					GameGUI.battle.updateSIP(((ShipPanelLeft)e.getComponent()).getShip());

				}//mouseClicked
			});


			leftShips.add(spl);
		}//for

		for(Ship b: r){

			ShipPanelRight spr = new ShipPanelRight(b);


			spr.addMouseListener(new MouseAdapter(){

				//when the panel is clicked
				public void mouseClicked(MouseEvent e){

					//to see if you can attack
					if(BattlePanel.selectedWeapon != null){

						if(isOnLeft(BattlePanel.turn.getShip())){

							BattlePanel.turn.getShip().attack(BattlePanel.selectedWeapon, ((ShipPanelRight)e.getComponent()).getShip());

							if(((ShipPanelRight)e.getComponent()).getShip().getHull() == 0){
								
								GameGUI.battle.kill(((ShipPanelRight)e.getComponent()).getShip());
								((ShipPanelRight)e.getComponent()).setVisible(false);
							}//if

							((ShipPanelRight)e.getComponent()).update();
							BattlePanel.sip.updateActionPoints();
							return;
						}//if
					}//if

					GameGUI.battle.updateSIP(((ShipPanelRight)e.getComponent()).getShip());

				}//mouseClicked
			});

			rightShips.add(spr);
		}//for

		Iterator<ShipPanelLeft> left = leftShips.iterator(); //used to go through the left ships
		Iterator<ShipPanelRight> right = rightShips.iterator(); //used to go through the right ships

		for(int i = 0; i < 4; i++){

			if(left.hasNext())
				this.add(left.next());

			else
				this.add(new ShipPanelLeft(null));

			this.add(new JPanel());
			this.add(new JPanel());

			if(right.hasNext())
				this.add(right.next());

			else
				this.add(new ShipPanelRight(null));		
		}//for
	}//default constructor


	//selects the ship passed in
	public void select(Ship s){


		for(ShipPanelLeft spl: leftShips){

			if(spl.getShip().equals(s)){

				spl.select();
				return;
			}//if
		}//for


		for(ShipPanelRight spr: rightShips){

			if(spr.getShip().equals(s)){

				spr.select();
				return;
			}//if
		}//for
	}//select


	//returns if the passed in ship is on the left
	public boolean isOnLeft(Ship s){

		for(ShipPanelLeft spl: leftShips)
			if(spl.getShip().equals(s))
				return true;

		return false;
	}//isOnLeft


	//returns if the passed in ship is on the right
	public boolean isOnRight(Ship s){

		for(ShipPanelRight spr: rightShips)
			if(spr.getShip().equals(s))
				return true;

		return false;
	}//isOnRight


	//deselects the ship passed in
	public void deselect(Ship s){


		for(ShipPanelLeft spl: leftShips){

			if(spl.getShip() == s){

				spl.deselect();
				return;
			}//if
		}//for


		for(ShipPanelRight spr: rightShips){

			if(spr.getShip() == s){

				spr.deselect();
				return;
			}//if
		}//for
	}//deselect


	//removes the ship
	public void remove(Ship s){

		ShipPanelLeft spl = new ShipPanelLeft(s);
		ShipPanelRight spr = new ShipPanelRight(s);

		if(leftShips.contains(spl))
			leftShips.get(leftShips.indexOf(spl)).setVisible(false);

		else
			rightShips.get(rightShips.indexOf(spr)).setVisible(false);
	}//remove
}//ShipBattlePanel